#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <unistd.h>

using namespace std;

namespace user_protocol
{
    const string SEP = "\r\n";
    // 加报头
    std::string Encode(const string &inbuffer)
    {
        string str = to_string(inbuffer.size());
        str += SEP;
        str += inbuffer;
        str += SEP;

        return str;
    }
    // "len"\r\n"{
    // "len"\r\n"{             }"
    // "len"\r\n"{             }"\r\n;
    // "len"\r\n"{             }"\r\n"len";
    // "len"\r\n"{             }"\r\n"len"\r\n"{             }";
    // "len"\r\n"{             }"\r\n"len"\r\n"{             }"\r\n

    // 去报头后看返回值就可以知道是否为一个完整的报文
    std::string Decode(string &inbuffer)
    {
        auto pos = inbuffer.find(SEP);
        if (pos == string::npos)
            return string();
        string packagestr = inbuffer.substr(0, pos);
        if (packagestr.empty())
            return string();
        int packagelen = stoi(packagestr);

        int total = packagelen + packagestr.size() + 2 * SEP.size();

        if (inbuffer.size() < total)
            return string();

        string ret = inbuffer.substr(pos + SEP.size(), packagelen);
        inbuffer.erase(0, total);
        return ret;
    }

    class Request
    {
    public:
        Request()
        {
        }
        Request(int x, int y, char oper)
            : _x(x), _y(y), _oper(oper)
        {
        }
        ~Request()
        {
        }
        bool Serialization(string *out)
        {
            Json::Value root;
            root["_x"] = _x;
            root["_y"] = _y;
            root["_oper"] = _oper;

            Json::FastWriter writer;
            *out = writer.write(root);
            return true;
        }
        bool DeSerialization(const string &in)
        {
            Json::Value root;
            // 把字符串从系统中读出来 ——————反序列化
            Json::Reader reader;
            // 把文件流中的数据反序列化到 root 中
            bool ret = reader.parse(in, root);
            if (!ret)
                return ret;

            _x = root["_x"].asInt();
            _y = root["_y"].asInt();
            _oper = root["_oper"].asInt();

            return ret;
        }

    public:
        int _x;
        int _y;
        char _oper;
    };

    class Response
    {
    public:
        Response()
        {
        }
        Response(int result, int code)
            : _result(result), _code(code)
        {
        }
        ~Response()
        {
        }
        bool Serialization(string *out)
        {
            Json::Value root;
            root["_result"] = _result;
            root["_code"] = _code;

            Json::FastWriter writer;
            *out = writer.write(root);
            return true;
        }
        bool DeSerialization(const string &in)
        {
            Json::Value root;
            // 把字符串从系统中读出来 ——————反序列化
            Json::Reader reader;
            // 把文件流中的数据反序列化到 root 中
            bool ret = reader.parse(in, root);
            if (!ret)
                return ret;

            _result = root["_result"].asInt();
            _code = root["_code"].asInt();

            return ret;
        }

    public:
        int _result;
        int _code; // 结果代码 0: success 1:除0错误   2: 非法操作   34......
    };

    class Factory
    {
    public:
        Factory()
        {
            srand(time(nullptr)^ getpid());
            _opers = "+-*/%";
        }
        shared_ptr<Request> BuildRequest()
        {
            int x = rand() % 10 + 20;
            usleep(x * 10);
            int y = rand() % 5; // [0,1,2,3,4]
            usleep(y * x * 5);
            char oper = _opers[rand() % _opers.size()];
            shared_ptr<Request> req = make_shared<Request>(x, y, oper);
            return req;
        }

        shared_ptr<Response> acceptResponse()
        {
            return make_shared<user_protocol::Response>();
        }
        ~Factory()
        {
        }

    private:
        string _opers;
    };
}
